package com.by.rpc.serializer;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Hessian序列化器
 *
 * @author lzh
 */
public class HessianSerializer implements Serializer {

    @Override
    public <T> byte[] serialize(T object) throws IOException {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        HessianOutput output = new HessianOutput(arrayOutputStream);
        output.writeObject(object);
        return arrayOutputStream.toByteArray();
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> type) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        HessianInput input = new HessianInput(byteArrayInputStream);
        return (T) input.readObject(type);
    }
}
